package picobot.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;

import picobot.interfaces.core.IMap;
import picobot.interfaces.core.IMapBuilder;
import picobot.map.MapBuilder;

/**
 * a class that performs a load map action
 */
public class LoadMapAction implements ActionListener {

	JFileChooser fc;
	PicobotUserInterface pui;

	/**
	 * the constructor for the LoadMapACtion class, which takes a
	 * PicobotUserInterface
	 * 
	 * @param pui
	 *            is the PicobotUserInterface
	 */
	public LoadMapAction(PicobotUserInterface pui) {
		this.pui = pui;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		fc = new JFileChooser();
		fc.setCurrentDirectory(new java.io.File("."));

		int returnVal = fc.showOpenDialog(fc);

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fc.getSelectedFile();

			IMapBuilder mb = new MapBuilder();
			mb.parseMap(file);
			IMap toLoadMap = mb.getCurrentMap();

			pui.getSimulator().loadMap(toLoadMap);
			pui.setPicoPanel();
		}

	}

}